package sample.controls;

import org.dreamwork.jasmine2.template.impl.XMLTemplateParser;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-9-15
 * Time: 17:12:59
 */
public class SyntaxRendererTemplateParser extends XMLTemplateParser {
    @Override
    public boolean validateTemplate (String content) {
        try {
            parse (content);
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Source source = new StreamSource (getClass ().getResourceAsStream("syntax.xsd"));
            Schema schema = factory.newSchema (source);
            source = new DOMSource (root);
            schema.newValidator ().validate (source);
            return true;
        } catch (SAXException ex) {
            throw new RuntimeException (ex.getMessage ());
        } catch (Exception ex) {
            return false;
        }
    }
}